package com.njupt.backtracking;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

/**
 * @Author: wujiaming
 * @CreateTime: 2024/12/17 18:04
 * @Description: 46. 全排列
 * @Version: 1.0
 */


public class Permute_46 {

    public List<List<Integer>> permute(int[] nums) {
        List<List<Integer>> results = new ArrayList<>();
        List<Integer> path = new LinkedList<>();
        boolean[] used = new boolean[nums.length];

        tracebacking(nums,used,results,path);
        return results;
    }

    private void tracebacking(int[] nums, boolean[] used, List<List<Integer>> results, List<Integer> path) {

        if(nums.length == path.size()){
            results.add(new ArrayList(path));
            return;
        }

        for (int i = 0; i < used.length; i++) {
            if(used[i] == true){
                continue;
            }

            used[i] = true;
            path.add(nums[i]);
            tracebacking(nums,used,results,path);
            path.remove(path.size()-1);
            used[i] = false;
        }
    }

    public static void main(String[] args) {
        int[] nums = {1,2,3};
        Permute_46 test = new Permute_46();
        List<List<Integer>> results = test.permute(nums);
        System.out.println(results);

    }
}
